package com.gexiaobao.pigeon.ui.view

import android.text.InputFilter
import android.text.Spanned

/**
 * @Author      : hxw
 * @Date        : 2024/9/10 17:09
 * @Describe    :
 */
class ChineseInputFilter: InputFilter {

    override fun filter(source: CharSequence, start: Int, end: Int, dest: Spanned, dstart: Int, dend: Int): CharSequence? {
        for (i in start until end) {
            val c = source[i]
            if (!isChineseCharacter(c)) {
                // 如果字符不是中文字符，返回空字符串，禁止输入该字符
                return ""
            }
        }
        return null
    }

    // 判断字符是否为中文字符
    private fun isChineseCharacter(c: Char): Boolean {
        // 中文字符的 Unicode 范围
        return c.toInt() in 0x4E00..0x9FAF
    }

}